<?php

class ajaxActions extends sfActions {

    public function executeSearch(sfWebRequest $request){
        if(!$request->isXmlHttpRequest()){ $this-> forward404(); }

        $advertiser = $request->getParameter('advertiser');
        $item = $request->getParameter('item');
        $yell_page = $request->getParameter('yell_page');

        $c = new Criteria();
        $pager = null;
        
        if($request->hasParameter('page')){
            $page = $request->getParameter('page');
            $this->getUser()->setAttribute('pagination_page', $page);
        }else{
            if($this->getUser()->hasAttribute('pagination_page')){
                $page = $this->getUser()->getAttribute('pagination_page');
            }else{
                $page = $request->getParameter('page', 1);
                $this->getUser()->setAttribute('pagination_page', $page);
            }
        }

        if($advertiser!="" && $item==""){
            $c->clear();
            $c->add(AdvertiserPeer::NOMBRE, "%".$advertiser."%", Criteria::LIKE);
            if($yell_page!=""){
                $c->add(AdvertiserPeer::PAG, $yell_page);
                $this->setTemplate('search_all');
            }
            $c->addAscendingOrderByColumn(AdvertiserPeer::PAG);
            $c->addAscendingOrderByColumn(AdvertiserPeer::NOMBRE_RUBRO);
            $c->addAscendingOrderByColumn(AdvertiserPeer::NOMBRE);

            $pager = new sfPropelPager('Advertiser', sfConfig::get('app_max_per_page'));
            $pager->setCriteria($c);
            $pager->setPage($page);
            $pager->init();

            if($pager->getNbResults()==1){
                $results = $pager->getResults();
                //$this->redirect('@detail?page='.$results[0]->getPag());
                //return $this->redirect("@homepage"."#".$results[0]->getPag());
                /*$this->yell_page = $results[0]->getPag();
                $this->setTemplate('search_by_page');
                return sfView::SUCCESS;*/
            }

            $this->setTemplate('search_by_advertiser');
        }
        
        if($item!="" && $advertiser==""){
            $c->clear();
            $c->add(RelatedItemPeer::NOMBRE_RUBRO_BUSQUEDA, "%".$item."%", Criteria::LIKE);
            if($yell_page!=""){
                $c->add(RelatedItemPeer::NUMERO_PAGINA, $yell_page);
                $this->setTemplate('search_all');
            }
            $c->addAscendingOrderByColumn(RelatedItemPeer::NUMERO_PAGINA);
            $c->addAscendingOrderByColumn(RelatedItemPeer::NOMBRE_RUBRO_BUSQUEDA);
            $c->addAscendingOrderByColumn(RelatedItemPeer::NOMBRE_RUBRO_RESULTADO);

            $pager = new sfPropelPager('RelatedItem', sfConfig::get('app_max_per_page'));
            $pager->setCriteria($c);
            $pager->setPage($page);
            $pager->init();

            if($pager->getNbResults()==1){
                $results = $pager->getResults();
                //$this->redirect('@detail?page='.$results[0]->getNumeroPagina());
                //return $this->redirect("@homepage"."#".$results[0]->getNumeroPagina());
                /*$this->yell_page = $results[0]->getNumeroPagina();
                $this->setTemplate('search_by_page');
                return sfView::SUCCESS;*/
            }

            $this->setTemplate('search_by_item');
        }

        if($advertiser!="" && $item!=""){
            $c->clear();
            $c->add(AdvertiserPeer::NOMBRE, "%".$advertiser."%", Criteria::LIKE);
            $c->add(AdvertiserPeer::NOMBRE_RUBRO, "%".$item."%", Criteria::LIKE);
            if($yell_page!=""){
                $c->add(AdvertiserPeer::PAG, $yell_page);
            }
            $c->addAscendingOrderByColumn(AdvertiserPeer::PAG);
            $c->addAscendingOrderByColumn(AdvertiserPeer::NOMBRE_RUBRO);
            $c->addAscendingOrderByColumn(AdvertiserPeer::NOMBRE);
            
            $pager = new sfPropelPager('Advertiser', sfConfig::get('app_max_per_page'));
            $pager->setCriteria($c);
            $pager->setPage($page);
            $pager->init();

            if($pager->getNbResults()==1){
                $results = $pager->getResults();
                //$this->redirect('@detail?page='.$results[0]->getPag());
                //return $this->redirect("@homepage"."#".$results[0]->getPag());
                /*$this->yell_page = $results[0]->getPag();
                $this->setTemplate('search_by_page');
                return sfView::SUCCESS;*/
            }

            $this->setTemplate('search_all');
        }

        if($pager==null){ $this->redirect('@homepage'); }
        $this->pager = $pager;
        return sfView::SUCCESS;
    }

    public function executeLoad_share_buttons(sfWebRequest $request){
        $url = $request->getParameter('url');
        $this->url = $url;
    }

}